package cn.bonoon.controllers;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.batatasframework.rawdata.RawdataDefinition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cn.bonoon.core.RawdataExportService;
import cn.bonoon.core.RawdataStreamService;
import cn.bonoon.entities.RawdataExportEntity;
import cn.bonoon.entities.RawdataImportEntity;
import cn.bonoon.kernel.annotations.QueryExpression;
import cn.bonoon.kernel.events.ReadEvent;
import cn.bonoon.kernel.security.LogonUser;
import cn.bonoon.kernel.web.ButtonEventType;
import cn.bonoon.kernel.web.ButtonRefreshType;
import cn.bonoon.kernel.web.annotations.grid.GridStandardDefinition;
import cn.bonoon.kernel.web.controllers.AbstractGridController;
import cn.bonoon.kernel.web.handlers.loader.AbstractLoaderHandler;

/**
 * 
 * 异常数据导出
 * 对应{@link RawdataExportEntity}数据库表
 * @author jackson
 *
 */
@Controller
@RequestMapping("s/rdi/{catalog}!export")
public class RawdataExportController extends AbstractGridController<RawdataExportEntity, RawdataExportItem> {
	
	private @Autowired RawdataStreamService rawdataStreamService;
	private RawdataExportService rawdataExportService;
	@Autowired
	public RawdataExportController(RawdataExportService rawdataExportService) {
		super(rawdataExportService);
		this.rawdataExportService = rawdataExportService;
	}

//	@Autowired
//	RawdataImportMaterialService rawdataImportMaterialService;
	private String catalog;
	
	@ModelAttribute
	public void setCatalog(@PathVariable("catalog") String catalog) {
		this.catalog = catalog;
	}

	@GridStandardDefinition( deleteOperation = false, autoOperation = true )
	@Override
	//通过x.larg0 is not null 来判断export实体的文件是存疑退回文件还是异常文件
	@QueryExpression("x.ownerId={USER owner} and x.larg0 is not null and x.deleted=0 ")
	protected RawdataExportService initLayoutGrid(LayoutGridRegister register) throws Exception {
		
		register.button("查看详情", "detail.dlg", ButtonEventType.DIALOG, ButtonRefreshType.REFRESH);		
		register.button("导出Excel", "download.do", ButtonEventType.JUMP, ButtonRefreshType.FINISH);
		
		return rawdataExportService;
	}
	
	@Override
	protected void __initLoader(AbstractLoaderHandler loadHandler, ReadEvent event) throws Exception {
		loadHandler.addExpression(" x.catalog='" + catalog + "'");
	}
	
	// 查看详情
	@RequestMapping("!{key}/detail.dlg")
	public String detail(Model model,
			@RequestParam("id") Long id,
			@RequestParam("gridid") String gridid,
			@PathVariable("key") String key) {
		
		model.addAttribute("layout", "layout-empty.vm");
		model.addAttribute("gridid", gridid);
		model.addAttribute("subject", id);
		RawdataExportEntity entity = rawdataExportService.get(id);
		model.addAttribute("entity",entity);

		LogonUser user = getUser();
		RawdataImportEntity subject = entity.getSubject();
		RawdataDefinition def = rawdataStreamService.getDefinition(user, subject.getCatalog(), subject.getKey());
		model.addAttribute("detailItemPath", def.getVmpath());
		List<Object> items=rawdataExportService.getItems(user, entity,def);
		model.addAttribute("items",items);
		model.addAttribute("materials", subject.getMaterials());
		model.addAttribute("materialsSize", subject.getMaterials().size());
		model.addAttribute("size",items.size());
		model.addAttribute("selectable",false);
		model.addAttribute("haohaochi",def.getKey());
		model.addAttribute("result",false);

		return "plugs-rawdata/details";
	}
	
	@RequestMapping("!{key}/page.change")
	public String page(@RequestParam("subject") Long id,int start,int end,
			HttpServletRequest request, Model model) {
		RawdataExportEntity entity = rawdataExportService.get(id);
		LogonUser user = getUser();
		RawdataImportEntity subject = entity.getSubject();
		RawdataDefinition def = rawdataStreamService.getDefinition(user, subject.getCatalog(), subject.getKey());
		List<Object> list=rawdataExportService.getItemsByStartEnd(user, entity, def, start, end);
		model.addAttribute("items", list);
		model.addAttribute("layout", "layout-empty.vm");
		return def.getVmpath();
	}
	//导出Excel
	@RequestMapping(value = "!{key}/download.do")
	public void download(HttpServletResponse response,
			HttpServletRequest request, 
			@RequestParam("id") Long id) {
		RawdataExportEntity entity = rawdataExportService.getByIdAndAdd(id);
		LogonUser user = getUser();
		RawdataDefinition def = rawdataStreamService.getDefinition(user, entity.getCatalog(), entity.getKey());
		rawdataExportService.excelExport(entity, response, request, def);

	}
}
